﻿using BCC.Common;
using BCC.Common.AuthorizationUtils;
using BCC.Model;
using BCC.Model.chat;
using BCC.Model.ModelsOut;
using BCC.Model.user;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Principal;
using System.Text;
using UChatinfo = BCC.Model.chat.UChatinfo;

namespace BCC.ViewModel.chat
{
    public class Getchat
    {
        protected static bccContext context = new Model.bccContext();
        public static string serverAddress = ConfigurationManager.AppSettings["serverAddress"].Trim();

        /// <summary>
        /// 根据账号查询聊天信息
        /// </summary>
        /// <param name="sendAccount"></param>
        /// <param name="receiveAccount"></param>
        /// <returns></returns>
        public static List<UChatinfo> selChatinfo(string sendAccount, string receiveAccount)
        {
            
            try
            {
                #region 之前代码
                //var chatInfos = context.UChatinfo.Where(u => ((u.SendAccount == sendAccount && u.ReceiveAccount == receiveAccount) || u.SendAccount == receiveAccount && u.ReceiveAccount == sendAccount) && !u.IsDelete).AsNoTracking().ToList();
                #endregion

                var url = serverAddress + $"api/Chat/selChatinfo?sendAccount={sendAccount}&receiveAccount={receiveAccount}";
                var rs = AuthorizationUtils.HttpAuthorizationGet(url);

                var chatinfo = JsonConvert.DeserializeObject<BaseOut>(rs);
                if (chatinfo.code == "true" && chatinfo.content != null)
                {
                    return JsonConvert.DeserializeObject<List<UChatinfo>>(chatinfo.content.ToString());
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 添加聊天信息
        /// </summary>
        /// <param name="uChatinfo"></param>
        /// <returns></returns>
        public static int addChatinfo(UChatinfo uChatinfo)
        {
           
            try
            {
                #region 之前代码
                //context.UChatinfo.Add(uChatinfo);
                //return context.SaveChanges();
                #endregion

                var url = serverAddress + $"api/Chat/addChatinfo";
                var rs = AuthorizationUtils.HttpAuthorizationPost(url, uChatinfo);

                var baseOut = JsonConvert.DeserializeObject<BaseOut>(rs);
                if (baseOut.code == "true" && baseOut.content != null)
                {
                    return int.Parse(baseOut.content.ToString());
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 根据账号查询未读聊天信息
        /// </summary>
        /// <param name="sendAccount"></param>
        /// <param name="receiveAccount"></param>
        /// <returns></returns>
        public static List<UChatinfo> selChatAllUnreadinfo(string receiveAccount)
        {

            try
            {
                var url = serverAddress + $"api/Chat/selChatAllUnreadinfo?receiveAccount={receiveAccount}";
                var rs = AuthorizationUtils.HttpAuthorizationGet(url);

                var chatinfo = JsonConvert.DeserializeObject<BaseOut>(rs);
                if (chatinfo.code == "true" && chatinfo.content != null)
                {
                    return JsonConvert.DeserializeObject<List<UChatinfo>>(chatinfo.content.ToString());
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
